<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of class
 *
 * @author NTC
 */
class Topic {

    //put your code here
    function get_topic_info($topic_id) {
        global $db;
        $sql = 'SELECT * FROM `detai` WHERE `id`="' . $topic_id . '"';
        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0)
            return $rows[0];
        return null;
    }

    function get_all_topic_info($page_list) {
        global $db;
        $start = USERS_LIST_TOTAL * ($page_list - 1);
        $sql = 'SELECT * FROM detai ORDER BY name ASC limit ' . $start . ', ' . USERS_LIST_TOTAL;
        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0)
            return $rows;
        return null;
    }

    function get_page_count() {
        global $db;
        $sql = 'SELECT count(*) FROM detai';

        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0) {
            if (intval($rows[0][0] / USERS_LIST_TOTAL) == $rows[0][0] / USERS_LIST_TOTAL) {
                return intval($rows[0][0] / USERS_LIST_TOTAL);
            } else {
                return intval($rows[0][0] / USERS_LIST_TOTAL) + 1;
            }
        }

        return null;
    }

    function get_sv_count($topicId) {
        global $db;
        $sql = 'SELECT count(*) FROM sinhvien_info WHERE detai_id = "'.$topicId.'"';

        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0) {
            if (intval($rows[0][0] / USERS_LIST_TOTAL) == $rows[0][0] / USERS_LIST_TOTAL) {
                return intval($rows[0][0] / USERS_LIST_TOTAL);
            } else {
                return intval($rows[0][0] / USERS_LIST_TOTAL) + 1;
            }
        }

        return null;
    }

    function get_topics_by_adviser($giaovien_id) {
        global $db;
        $sql = 'SELECT * FROM detai where `giaovien_id`==' . $giaovien_id;
        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0)
            return $rows;
        return null;
    }

    function get_name_by_id($topicId){
        global $db;
        $sql = 'SELECT * FROM detai where `id`=' . $topicId;
        $rows = $db->query($sql, $fetchArray = true);
        if (count($rows) > 0)
            return $rows[0]["name"];
        return null;
    }

    function is_exist($tenDT, $except_id=0) {
        global $db;
        $sql = "SELECT count(*) from detai WHERE `name`='" . $tenDT . "' && id!=" . $except_id;
        $rows = $db->query($sql, $fetArray = true);
        $count = $rows[0][0];
        return $count;
    }

    function insert_topic($userid, $tenDT, $thongtinDT) {
        $check_reg = $this->check_register($userid, $tenDT, $thongtinDT);
        if ($check_reg != "OK") {
            throw new Exception($check_reg);
        }
        try {
            if ($this->is_exist($tenDT)) {
                throw new Exception("Topic is exists!");
            }

            global $db;
            $sql = "INSERT INTO detai (`giaovien_id`, `name`, `thongtin_detai`,`status`)
                    VALUES ('" . $userid . "', '" . $tenDT . "', '" . $thongtinDT . "', '" .'1'. "')";
            $db->query($sql, $fetchArray = false);
        } catch (Exception $e) {
            throw $e;
        }
    }

    static function check_register($userid, $tenDT, $thongtinDT) {
        $msg = "OK";
        if ($tenDT == "" || $thongtinDT == "") {
            $msg = "Ten de tai or Thong tin de tai is null!";
        }

        return $msg;
    }

    static function get_students_count($topicId,$except_id=0) {
        global $db;
        $sql = 'SELECT count(*) from sinhvien_info WHERE `detai_id`=' . $topicId;        
        $rows = $db->query($sql, $fetArray = true);
        $count = $rows[0][0];
        return $count;
        
    }

    function check_modify($topicName) {

        if ($topicName == "") {
            $msg = "Topic's name is null!";
            return $msg;
        }

        return "OK";
    }

    function edit_exist_topic($topic_id, $topicName, $topicInfor) {
        $check_modify = $this->check_modify($topicName);
        if ($check_modify != "OK") {
            throw new Exception($check_modify);
        }

        try {


            global $db;
            $sql = "UPDATE detai
                    SET `name`='" . $topicName .
                    "', `thongtin_detai`='" . $topicInfor .
                    "' WHERE id=" . $topic_id;
            $db->query($sql, $fetchArray = false);
        } catch (Exception $e) {
            throw $e;
        }
    }

    function id_exist($topicId, $except_id=0) {
        global $db;
        $sql = "SELECT count(*) from detai WHERE `id`='" . $topicId . "' && id!=" . $except_id;
        $rows = $db->query($sql, $fetArray = true);
        $count = $rows[0][0];
        return $count;
    }

    function delete_topic($topicId) {
        global $db;
        if (!($this->id_exist($topicId))) {
            throw new Exception("Topic not found!");
        }

        try {
            if (have_permission($groupid = 1)) {//Have adviser permission
                $sql = "SELECT * from sinhvien_info";               
                $rows = $db->query($sql, $fetArray=true);
                for ($i = 0; $i < count($rows); ++$i) {
                    $sql1 = "UPDATE sinhvien_info SET `detai_id`= 0 WHERE detai_id ='" .$topicId."'";
                    $db->query($sql1, $fetchArray=false);
                }
                
                $sql = "DELETE from detai WHERE id='" . $topicId . "'";
                $db->query($sql, $fetchArray = false);
                
                return true; //DEL COMPLETED
            }
        } catch (Exception $e) {
            throw $e;
        }
    }

    function get_student_count($topicId) {
        return 1;
    }

    function set_student_for_topic($topicId,$svId,$status){
        try {
            global $db;
            $sql = "UPDATE detai
                    SET `status`='" . $status .
                    "', `accept_student`='" . $svId .
                    "' WHERE id=" . $topicId;
            $db->query($sql, $fetchArray = false);
        } catch (Exception $e) {
            throw $e;
        }
    }

}

?>
